var URL = 'http://cqwind.chinacloudapp.cn:8083';
//var URL = 'http://192.168.101.194:8083/';
//设置本地存储
function setStore(name, objData) {
	var str = JSON.stringify(objData);
	localStorage.setItem(name, str);
}
//获取本地存储
function getStore(name) {
	var obj = JSON.parse(localStorage.getItem(name));
	return obj;
}
//补零函数
function toDou(n) {
	return n < 10 ? '0' + n : '' + n;
}

//选项卡
function Tab(obj) {
	var index = $(obj).index();
	$(obj).addClass('tab-btn--active').siblings().removeClass('tab-btn--active');
	$('.tab-item').eq(index).addClass('tab-item--acitve').siblings().removeClass('tab-item--acitve');
}

//加载地址
function locationDetail(){
	window.location.href='../index.html';
}

//设置下拉选项
function setDropData(select,data){
	var option=''
	for (var i = 0; i < data.length; i++) {
		option += `
		<option value="${data[i].name}" ${data[i].selected && featureData[i].selected }>${data[i].value}</option>
	`
	}
	select.append(option);
}

//可以设置月,小时
function setDropNum(select,num){
	var option='';
	for (var i = 1; i <= num; i++) {
		option += `
		<option value="${i}">${i}</option>
	`;
	}
	select.append(option);
}
//根据月份改变天
function changeDay(obj,select) {
		var totalDay = 0;
		var val = $(obj).val();
		
		if (val == 1 || val == 3 || val == 5 || val == 7 || val == 8 || val == 10 || val == 12) {
			totalDay = 31;
		} else if (val == 2) {
			totalDay = 28;
		} else {
			totalDay = 30;
		}

		setDropNum(select,totalDay);

		//select.html ('')		
}

//图表构造函数
function Chart(chartId) {
	this.chart = document.getElementById(chartId);
	this.myChart = echarts.init(this.chart);
	this.myChart.setOption({
		title: {
			text: '功率曲线'
		},
		tooltip: {},
		legend: {
			data: ['GP', 'GP2']
		},
		xAxis: {
			type: 'value',
			data: []
		},
		yAxis: {
			type: 'value',
			//min: -40,
			splitLine: {
				lineStyle: {
					type: 'dashed'
				}
			}
		},
		series: [{
			name: 'GP',
			type: 'scatter',
			data: [],
			smooth: true
		}, {
			name: 'GP2',
			type: 'scatter',
			data: [],
			smooth: true
		}]
	});
}
Chart.prototype = {
	constructor: Chart,
	dataArr: [],
	dataArr2: [],
	loadData: function() {
		this.myChart.setOption({
			/*xAxis: {
				    	type: 'value',
				        data: res[0].WSbin
				    },*/
			series: [{
				// 根据名字对应到相应的系列
				name: 'GP',
				//data: res[0].GP
				data: this.dataArr
			}, {
				// 根据名字对应到相应的系列
				name: 'GP2',
				//data: res[0].GP
				data: this.dataArr2
			}]
		});
	}
}

//显示表格
function ViewTable(container,headerData,tableData){
	this.container=container;
	this.headerData=headerData;
	this.tableData=tableData;
	this.loadHeader();
}
ViewTable.prototype={
	constructor:ViewTable,
	loadHeader:function(){
		var header = '';
		var table = `
			<table>
				<thead>
					<tr class="header"></tr>
				</thead>
				<tbody class="contain">
					
				</tbody>
			</table>
			`;
		this.container.append(table);
		for (var name in this.headerData) {
			header += `
				<th _name="${name}" width="100">${this.headerData[name]}</th>
			`;
		}
		this.container.find('.header').append(header);
	},
	loadBody:function(tableData){
		var tableData=tableData || this.tableData;
		var row = '';
		var column = '';
		for (var i = 0; i <tableData.length; i++) {
			row += '<tr class="row"></tr>';
			this.container.find('.contain').append(row);
			row = '';
			column = '';
			for (var name in this.headerData) {
				if(name=='history'){
					column += `<td><a href="javascript:;" class="jump" _cnname="${tableData[i]['user_def_mid']}" _enname="${tableData[i]['user_def_name']}">查看详情</a></td>`;
				}if(name=='user_def_name'){
					column += `<td>${this.translateLanguage(tableData[i][name])}</td>`;
				}else if(name=='describe'){
					column += `<td>${this.addDescription(tableData[i],tableData[i]['user_def_name'])}</td>`;
				}else{
					column += `<td class="${name}">${tableData[i][name]}</td>`;
				}	
			}
			this.container.find('.row').eq(i).append(column);
			column = '';
		}
	},
	loadBody2:function(val){//有返回值的
		var val=val || null;
		var row = '';
		var column = '';
		var timeStamp=[];
		for (var i = 0; i <this.tableData.length; i++) {
			row += `<tr class="row" _mid="${this.tableData[i]['user_def_mid']}"></tr>`;
			this.container.find('.contain').append(row);
			row = '';
			column = '';
			if(val=='pba'){
				timeStamp[i]=this.tableData[i]['user_def_time'] || [];
			}else{
				timeStamp=[];
			}
			for (var name in this.headerData) {
				if(name=='history'){
					column += `<td><a href="javascript:;" class="jump" _cnname="${this.tableData[i]['user_def_mid']}" _enname="${this.tableData[i]['user_def_name']}">查看详情</a></td>`;
				}else{
					column += `<td class="${name}" title="${this.tableData[i][name]}">${this.tableData[i][name]}</td>`;
				}
				
			}
			this.container.find('.row').eq(i).append(column);
			column = '';
		}
		return timeStamp;
	},
	translateLanguage:function(name){
		switch(name){
			case 'yaw':
				return '偏航对风不正';
				break;
			case 'generator':
				return '发电机温度异常';
				break;
			case 'generatorb':
				return '发电机轴承异常';
				break;
			case 'anemometer':
				return '风速仪异常';
				break;
			case 'blade':
				return '叶片结冰';
				break;
			case 'bladep':
				return '叶片污染';
				break;
			default:
				return name;
				break;
		}
	},
	addDescription:function(obj,name){
		switch(name){
			case 'yaw'://偏航对风不正
				var maxDir3=JSONfindArr(obj,'user_def_name',name,'max_dir_3');
				var rank=JSONfindArr(obj,'user_def_name',name,'rank');
				return `误差角度:${maxDir3},${rank}`;
				break;
			case 'blade'://叶片结冰
				var prob=JSONfindArr(obj,'user_def_name',name,'Prob');
				return `结冰概率:${prob}`;
				break;
			case 'generator'://发电机温度异常
				var vecStd=JSONfindArr(obj,'user_def_name',name,'vec_std');
				return `风险值:${vecStd}`;
				break;
			default:
				return '无状态';
				break;
		}
	}
}







